package sg.edu.nus.iss.billing.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Thureim, Yin Ni Tun, Zhou Zifeng
 */
public class FileUtils {

	public static boolean fileExists(String fileName) {
		File file = new File(fileName);
		if (file.exists()) {
			return true;
		}

		return false;
	}

	public static boolean wildcardMatch(String regEx, String input) {
		Pattern pattern = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(input);
		return matcher.find();
	}

	public static String generateTemplateString(String templatePath, Map<String, Object> valueMap) {
		try {
			return generateTemplateString(new FileInputStream(templatePath), valueMap);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String generateTemplateString(InputStream inputStream, Map<String, Object> valueMap) {
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
		StringBuffer stringBuf = new StringBuffer();
		String line = null;
		String crlf = System.getProperty("line.separator");
		try {
			while ((line = bufferedReader.readLine()) != null) {
				line = replaceTemplateStr(line, valueMap);
				stringBuf.append(line);
				stringBuf.append(crlf);
			}
			bufferedReader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return stringBuf.toString();
	}

	public static void generateTemplateFile(String filePath, String content) throws IOException {

		File distFile = new File(filePath);
		if (!distFile.getParentFile().exists())
			distFile.getParentFile().mkdirs();
		BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(distFile));
		bufferedWriter.write(content);
		bufferedWriter.flush();
		bufferedWriter.close();
	}

	public static void generateTemplateFile(InputStream inputStream, String filePath, Map<String, Object> valueMap)
			throws IOException {
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

		File distFile = new File(filePath);
		if (!distFile.getParentFile().exists())
			distFile.getParentFile().mkdirs();
		BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(distFile));

		String line = null;
		while ((line = bufferedReader.readLine()) != null) {
			line = replaceTemplateStr(line, valueMap);
			bufferedWriter.write(line);
			bufferedWriter.newLine();
		}
		bufferedWriter.flush();
		bufferedReader.close();
		bufferedWriter.close();
	}

	public static void generateTemplateFile(String templatePath, String filePath, Map<String, Object> valueMap)
			throws IOException {
		generateTemplateFile(new FileInputStream(templatePath), filePath, valueMap);
	}

	public static String replaceTemplateStr(String sourceStr, Map<String, Object> valueMap) {
		if (valueMap == null)
			return sourceStr;

		Pattern p;
		Matcher m;
		p = Pattern.compile("\\$\\{.*?\\}");
		m = p.matcher(sourceStr);
		StringBuffer sb = new StringBuffer();
		while (m.find()) {
			Object replacement = valueMap.get(sourceStr.substring(m.start() + 2, m.end() - 1));
			if (replacement != null) {
				try {
					if(replacement instanceof String)
					{
						m.appendReplacement(sb, replacement.toString());
					}else
					{
						double value = Double.parseDouble(replacement.toString());
						m.appendReplacement(sb, String.format("%.2f", value));
					}
				} catch (NumberFormatException e) {
					m.appendReplacement(sb, replacement.toString());
				}
					
					
			} else {
				m.appendReplacement(sb, "--");
			}
		}
		m.appendTail(sb);

		return sb.toString();
	}

}
